# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: shitake
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;
from unique import UNIQUE;

class RentSubsidyBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "RentSubsidy"+UNIQUE, dashboard=True);

    def wire(self):
        self.register_result_listener("Client", "SubmitInfo", self.submit_info_complete)
        self.register_result_listener("CaseManager", "Calculate", self.calculate_complete)
        self.register_result_listener("Supervisor", "Approve", self.approve_complete)
        #self.register_result_listener("Supervisor", "Approve", self.is_approval_denied_complete)
        self.register_result_listener("FiscalAgent", "SendCheck", self.send_check_complete)
      
    def submit_info_complete(self, results):
        for result in results:
            earnedincome=float(result.data["earnedincome"])
            cashbenefits=float(result.data["cashbenefits"])
            rentpayable=float(result.data["rentpayable"])
            totalincome=earnedincome+cashbenefits
            estsubsidy=rentpayable-totalincome/2
            result.data["totalincome"]=totalincome
            result.data["estsubsidy"]=estsubsidy
            task = Task.construct_from_result(result, "CaseManager", "Calculate");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def calculate_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Supervisor", "Approve");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def approve_complete(self, results):
        for result in results:
            if result.data['approvaltype'] == "FALSE":
                #Add task to redo the calculation
                task = Task.construct_from_result(result, "CaseManager", "Calculate");
                self.workflow.add(task);
                self.workflow.update_status(result, Status.COMPLETE);
            elif result.data['approvaltype'] == "TRUE":
                task = Task.construct_from_result(result, "FiscalAgent", "SendCheck");
                self.workflow.add(task);
                self.workflow.update_status(result, Status.COMPLETE);

    #def is_approval_denied_complete(self, results):
        #for result in results:
            #if result.data['ApprovalType'] == "NO":
                #Add task to redo the calculation
                #task = Task.construct_from_result(result, "CaseManager", "Calculate");
                #self.workflow.add(task);
                #Add task to ask if we need another prescription
                #task = Task.construct_from_result(result, "Physician", "IsPrescriptionNeeded");
                #task.set_field('prescriptionnumber',result.get_int_field('prescriptionnumber')+1);
                #task.remove_field('isprescriptionneeded');
                #self.workflow.add(task);
            #self.workflow.update_status(result, Status.COMPLETE);

    def send_check_complete(self, results):
        for result in results:
            #task = Task.construct_from_result(result, "FiscalAgent", "SendCheck");
            #self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

if __name__ == '__main__':
    backend = RentSubsidyBackend();